export function BMapGLLoader(ak) {
  return new Promise((resolve, reject) => {
    if (window.BMapGL) {
      resolve(window.BMapGL);
      return true;
    }else {
      const script = document.createElement('script')
      script.type = 'text/javascript'
      script.src = 'https://api.map.baidu.com/api?v=1.0&type=webgl&callback=initBMap&ak=' + ak
      script.async = true
      script.onerror = reject
      document.head.appendChild(script)
    }
    window.initBMap = function () {
      console.log("百度地图BMapGL脚本初始化成功...");

      resolve(window.BMapGL)
    }
  })
}

export function BMapLoadMapv(src) {
  return new Promise((resolve, reject) => {
    //console.log("in BMapLoadMapv(src)");
    if (window.mapv) {
      resolve(window.mapv);
      return true;
    }else {
      const script = document.createElement('script')
      script.type = 'text/javascript'
      script.src = 'https://mapv.baidu.com/build/mapv.js';
      //script.src = 'https:/api.map.baidu.com/build/mapv.js';
      script.async = false;
      script.onerror = reject
      document.head.appendChild(script);

/*      setTimeout(() => {
        resolve();
      }, 5000);*/

      console.log("百度地图mapv脚本加载成功...");
      //console.log(mapv);
      console.log(window.mapv);
      resolve(window.mapv)  ;
    }

  })

}


export function BMapLoader(ak) {
  return new Promise((resolve, reject) => {
    if (window.BMap) {
      resolve(window.BMap);
      return true;
    }else {
      const script = document.createElement('script')
      script.type = 'text/javascript'
      script.src = "https://api.map.baidu.com/api?v=2.0&ak="+ ak +"&s=1&callback=onBMapCallback";
      script.async = true
      script.onerror = reject
      document.head.appendChild(script)
      //console.log(script.src);
      //console.log(window.BMap);
    }
    window.onBMapCallback = function () {
      console.log("百度地图BMap脚本初始化成功...");
      //console.log(window.BMap);
      resolve(window.BMap)
    }
  })
}
